package com.awplanet.sitegen;

import com.awplanet.sitegen.data.*;
import com.awplanet.sitegen.utils.IOUtils;

import java.util.regex.Pattern;

/**
 * User: a_polyakov
 */
public enum EDataFormat {
	WORD {
		@Override
		public void modelAppendLine(Model model, String line) {
			// Armenia <-> Bolnisi <-> Privatbank <-> Sulkhan-saba St. 101 <-> 1100 <-> False <-> Monday:10:00 AM - 5:30 PM,Tuesday:10:00 AM - 5:30 PM,Wednesday:10:00 AM - 5:30 PM,Thursday:10:00 AM - 5:30 PM,Friday:10:00 AM - 5:30 PM,Saturday:10:00 AM - 1:30 PM,Sunday:Closed <-> money transfer,quick cash,quick pay
			boolean allowedFormats = false;
			int indexA = line.indexOf(" <-> ");
			if (indexA > 0) {
				final String countryName = line.substring(0, indexA);
				final String countryFileName = IOUtils.fileName(countryName);
				int indexB = line.indexOf(" <-> ", indexA + 5);
				if (indexB > 0) {
					final String cityName = line.substring(indexA + 5, indexB);
					final String cityFileName = IOUtils.fileName(cityName);
					indexA = line.indexOf(" <-> ", indexB + 5);
					if (indexA > 0) {
						final String departmentName = line.substring(indexB + 5, indexA);
						indexB = line.indexOf(" <-> ", indexA + 5);
						if (indexB > 0) {
							final String departmentAddress = line.substring(indexA + 5, indexB);
							indexA = line.indexOf(" <-> ", indexB + 5);
							if (indexA > 0) {
								final String departmentZipCode = line.substring(indexB + 5, indexA);
								indexB = line.indexOf(" <-> ", indexA + 5);
								if (indexB > 0) {
									final String departmentFlag1 = line.substring(indexA + 5, indexB);
									indexA = line.indexOf(" <-> ", indexB + 5);
									if (indexA > 0) {
										allowedFormats = true;
										final String departmentWorkingTime = line.substring(indexB + 5, indexA);
										final String departmentServices = line.substring(indexA + 5);

										Country country = model.getCountryMap().get(countryFileName);
										if (country == null) {
											country = new Country(countryFileName, countryName);
											model.getCountryMap().put(countryFileName, country);
										}

										City city = country.getCityMap().get(cityFileName);
										if (city == null) {
											city = new City(cityFileName, cityName);
											country.getCityMap().put(cityFileName, city);
										}

										Department department = new Department(departmentName, departmentAddress, departmentZipCode, departmentFlag1, "");
										city.getDepartmentMap().put(departmentName, department);

										if (departmentWorkingTime != null && !departmentWorkingTime.isEmpty()) {
											String workTimeArray[] = departmentWorkingTime.split(",");
											for (String workTime : workTimeArray) {
												int index = workTime.indexOf(':');
												if (index > 0 && index < workTime.length()) {
													DepartmentWorkTime departmentWorkTime = new DepartmentWorkTime(workTime.substring(0, index), workTime.substring(index + 1));
													department.getWorkTimeList().add(departmentWorkTime);
												} else {
													System.out.println("Error format column work time to line " + line);
												}
											}
										}

										if (departmentServices != null && !departmentServices.isEmpty()) {
											String serviceArray[] = departmentServices.split(",");
											for (String service : serviceArray) {
												DepartmentService departmentService = new DepartmentService(service);
												department.getServiceList().add(departmentService);
											}
										}

									}
								}
							}
						}
					}
				}
			}
			if (!allowedFormats) {
				System.out.println("Error format line " + line);
			}
		}
	},
	MONEYGRAM {
		private final Pattern separatorPattern = Pattern.compile(" - ", Pattern.LITERAL);

		@Override
		public void modelAppendLine(Model model, String line) {
			//MoneyGram - BANGLADESH - BRAMHAN BARIA - ISLAMI BANK AKHAURA SME BRANCH - #216 -  AKHAURA CHANDURA ROAD &nbsp;1ST FLOOR AKHAURA SME SERVICE CENTER NOORJAHAN MANJEEL BRAMHAN BARIA, 3400 01730326104   Mon-Thu 10:00-16:00 Fri-Sat 00:00-00:00 Sun 10:00-16:00
			boolean allowedFormats = false;
			int indexA = line.indexOf(" - ");
			if (indexA > 0) {
				int indexB = line.indexOf(" - ", indexA + 3);
				if (indexB > 0) {
					final String countryName = line.substring(indexA + 3, indexB);
					final String countryFileName = IOUtils.fileName(countryName);
					indexA = line.indexOf(" - ", indexB + 3);
					if (indexA > 0) {
						final String cityName = line.substring(indexB + 3, indexA);
						final String cityFileName = IOUtils.fileName(cityName);
						indexB = line.indexOf(" -  ", indexA + 3);
						if (indexB > 0) {
							final String departmentName = line.substring(indexA + 3, indexB);
							int indexWorkTime = line.indexOf("   ", indexB + 4);
							if (indexWorkTime > 0) {
								int indexPhone = line.lastIndexOf(" ", indexWorkTime - 1);
								if (indexPhone > 0) {
									int indexZipCode = line.lastIndexOf(", ", indexPhone - 1);
									if (indexZipCode > 0) {
										final String departmentAddress = line.substring(indexB + 4, indexZipCode);
										String departmentZipCode = "";
										if (indexPhone - indexZipCode > 5) {
											departmentZipCode = line.substring(indexZipCode + 2, indexPhone);
										}
										allowedFormats = true;
										final String departmentPhone = line.substring(indexPhone, indexWorkTime);

										Country country = model.getCountryMap().get(countryFileName);
										if (country == null) {
											country = new Country(countryFileName, countryName);
											model.getCountryMap().put(countryFileName, country);
										}

										City city = country.getCityMap().get(cityFileName);
										if (city == null) {
											city = new City(cityFileName, cityName);
											country.getCityMap().put(cityFileName, city);
										}

										Department department = new Department(departmentName, departmentAddress, departmentZipCode, "", departmentPhone);
										city.getDepartmentMap().put(departmentName, department);

										int i = indexWorkTime + 3;
										while (i > 0 && i < line.length()) {
											indexA = line.indexOf(' ', i);
											if (indexA > 0) {
												indexB = line.indexOf(' ', indexA + 1);
												if (indexB < 0) {
													indexB = line.length();
												}
												DepartmentWorkTime departmentWorkTime = new DepartmentWorkTime(line.substring(i, indexA), line.substring(indexA + 1, indexB));
												department.getWorkTimeList().add(departmentWorkTime);
												i = indexB + 1;
											}
										}
									}
								}
							}
						}
					}
				}
			}
			if (!allowedFormats) {
				System.out.println("Error format line " + line);
			}
		}
	};

	public abstract void modelAppendLine(Model model, String line);
}
